package demo.day11_CollectionAddpackage;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/8/1-08-01-18:17
 * @Description：demo.day11_CollectionAddpackage
 */
interface Action{
    String[] test(String str);
}

public class InterfaceImpl {
    public static void main(String[] args) {
        String[] str = new Action(){
            @Override
            public String[] test(String str) {
                String[] result = new String[str.length()];
                int index = 0;
                for (int i = 0; i < str.length(); i++) {
                    if(str.charAt(i) != '|'){
                        if(result[index]==null){
                            result[index] = "" + str.charAt(i);
                        }else{
                            result[index] = result[index] + str.charAt(i);
                        }
                    }else{
                        index++;
                    }
                }
                return result;
            }
        }.test("s|22|sf|23|sd3|de");
        for (String s : str) {
            if(s == null)
                break;
            System.out.println(s);
        }
        String test = "ee_e32_WE3EF_Eeff3";

        System.out.println("------------------------------");
        String[] str2 = new Action(){
            @Override
            public String[] test(String str) {
                Collection<String> result = new ArrayList();
                String temp = null;
                for (int i = 0; i < str.length(); i++) {
                    if(str.charAt(i) != '|'){
                        if(temp == null){
                            temp = "" + str.charAt(i);
                        }else{
                            temp = temp + str.charAt(i);
                        }
                    }else{
                        result.add(temp);
                        temp = null;
                    }
                }
//                System.out.println(result);
//                Arrays.copyOf(new String[]{result.toArray()},result.size();
//                String[] arr = new String[]{};
//                String[] arrnew = result.toArray(arr);
//                System.out.println("---" + Arrays.toString(arrnew));
                //return (String[])result.toArray();

                return result.toArray(new String[]{});
            }
        }.test("s|22|sf|23|sd3|de");
        for (String s : str2) {
            if(s == null)
                break;
            System.out.println(s);
        }
        System.out.println("------------------------------");
        String[] str3 = new Action(){
            @Override
            public String[] test(String str) {
//                StringBuilder builder = new StringBuilder();
//                String temp = null;
//                for (int i = 0; i < str.length(); i++) {
//                    if(str.charAt(i) != '-'){
//                        if(temp==null){
//                            temp = "" + str.charAt(i);
//                        }else{
//                            temp = temp + str.charAt(i);
//                        }
//                    }else{
//                        builder.append(temp);
//                        temp = null;
//                    }
//                }
//                System.out.println(builder.toString());
//                //return builder.toString();
                String[] result = new String[str.length()];
                int index = 0;
                for (int i = 0; i < str.length(); i++) {
                    if(str.charAt(i) != '-'){
                        if(result[index]==null){
                            result[index] = "" + str.charAt(i);
                        }else{
                            result[index] = result[index] + str.charAt(i);
                        }
                    }else{
                        index++;
                    }
                }
                return result;
            }
        }.test("we-342-dr3-sw4-wr-3rf");
        for(String s : str3){
            if (s == null)
                break;
            System.out.println(s);
        }
    }
}
